﻿using System;
using System.Windows.Forms;
using EzDesk.DevHelper.DbMgr.FeedStudio.ToEzLanguage;
using EzDesk.DevHelper.DbMgr.Presenters;
using EzDesk.DevHelper.DbMgr.ToEzLanguage;

namespace EzDesk.DevHelper.DbMgr.Views
{
	public partial class EzMLForm: Form, IEzMLView
	{
        EzMLPresenter _presenter;
        private bool _isLoaded;
        private string _sectionName;
        private string _content;

		public EzMLForm(string sectionName, string content, IFeedView feedForm)
		{
            _sectionName = sectionName;
            _content = content;

            _isLoaded = false;
            InitializeComponent();
            _presenter = new EzMLPresenter(this, feedForm, sectionName, content);
            _isLoaded = true;
		}


        /// <summary>
        /// 这里的Content是EZML
        /// </summary>
        /// <param name="sectionName"></param>
        /// <param name="content"></param>
	    public void DisplayFeedEzML(string sectionName, string content)
	    {
            EzLanguageBuilder builder = new EzLanguageBuilder(content, sectionName);
            string ezmlLanguage = builder.Build();
            txtEzML.Text = ezmlLanguage;
	    }


        private void btnCancel_Click(object sender, EventArgs e)
        {
            _presenter.ResetContent();
            CloseForm();
        }

        public void CloseForm()
        {
            Close();
            Dispose();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            CloseForm();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            _presenter.ResetContent();
        }

        private void txtEzML_TextChanged(object sender, EventArgs e)
        {
            if (_isLoaded)
            {
                _presenter.RefreshText(txtEzML.Text);
            }
        }

        private void EzMLForm_Load(object sender, EventArgs e)
        {
            DisplayFeedEzML(_sectionName, _content);
        }

        private void EzMLForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            _presenter.CloseForm();
        }
	}
}
